using System;
using SolarWinds.Input;

namespace SolarWinds.Widgets
{
    public abstract class Button : TiledWidget
    {
        public event EventHandler Pressed;
        public event EventHandler Released;
        public event EventHandler Clicked;
        
        #region Properties

        public bool Down { get; set; }
        public bool Checked { get; set; }
        public bool Checkable { get; set; }
        
        #endregion

        protected Button(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
        }

        protected internal override void MouseClickEvent(MouseEvent evt)
        {
            if (Checkable)
            {
                // toggle
                Checked = !Checked;

                evt.Consume();
            }

            if (Clicked != null)
            {
                Clicked(this, null);

                evt.Consume();
            }            
        }

        protected internal override void MousePressEvent(MouseEvent evt)
        {
            if (Pressed != null)
            {
                Pressed(this, null);

                evt.Consume();
            }
        }

        protected internal override void MouseReleaseEvent(MouseEvent evt)
        {
            if (Released != null)
            {
                Released(this, null);

                evt.Consume();
            }
        }

        protected internal override void KeyClickedEvent(KeyEvent evt)
        {
            bool wasClicked = false;

            if (Checkable)
            {
                if (evt.Key == Microsoft.Xna.Framework.Input.Keys.Space)
                {
                    // toggle
                    Checked = !Checked;

                    evt.Consume();

                    wasClicked = true;
                }
            }
            else
            {
                if (evt.Key == Microsoft.Xna.Framework.Input.Keys.Enter)
                {
                    wasClicked = true;
                }
            }

            if (wasClicked &&
                Clicked != null)
            {
                Clicked(this, null);

                evt.Consume();
            }
        }
    }
}